package com.twu.biblioteca.Menus;

import com.twu.biblioteca.MenuItems.*;
import com.twu.biblioteca.UserAccount;
import com.twu.biblioteca.UserChoice;

import java.util.*;

public class MainMenu extends Menu {

    private ArrayList<MenuItem> menuItems = new ArrayList<MenuItem>();

    public MainMenu() {

        description = "Main Menu";
        addItem(new BooksMenu());
        addItem(new MoviesMenu());
        addItem(new AccountMenuItem());
        addItem(new LoginMenuItem());
        addItem(new ExitMenuItem());
    }


    public boolean controller(UserAccount sessionUser) {

        addLoginLogoutOption(sessionUser);

        MenuPrinter menuPrinter = new MenuPrinter(this);
        menuPrinter.printMenu();

        UserChoice userChoice = new UserChoice();
        userChoice.setOptionsCount(menuItems.size());

        menuItems.get( userChoice.getChoice()-1).controller(sessionUser);

        return true;

    }

    private void addLoginLogoutOption(UserAccount sessionUser) {
        int index = menuItems.size()-2;
        menuItems.remove(index);
        if(sessionUser.isActive() )
            addItem(new LogoutMenuItem(),index);
        else
            addItem(new LoginMenuItem(),index);

    }

    @Override
    public void addItem(MenuItem menuItem) {
        menuItems.add(menuItem);
    }

    public void addItem(MenuItem menuItem,int index) {
        menuItems.add(index,menuItem);
    }

    @Override
    public Iterator createIterator() {
        return menuItems.iterator();
    }


}
